package checkers;

import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import chesskers.GamePiece;
import chesskers.ImagePanel;

public class CheckPiece extends GamePiece{
	
	public CheckPiece(boolean black, int x_, int y_) {
		super(black, x_, y_);
		
		if (black){
			movePossibilities = new int[][][]{{{1, 1}}, {{-1, 1}}};
			attackPossibilities = new int[][][]{{{2, 2}}, {{-2, 2}}};
		}else{
			movePossibilities = new int[][][]{{{1, -1}}, {{-1, -1}}};
			attackPossibilities = new int[][][]{{{2, -2}}, {{-2, -2}}};
		}
		
		
	}
	
	@Override
	public ArrayList<Point> getAttackPossibilities(GamePiece[][] board){
		
		ArrayList<Point> validAttacks = new ArrayList<Point>();
		
		ArrayList<Point> multiJumpLocs = new ArrayList<Point>();
		
		for (int i = 0; i < attackPossibilities.length; i++){
			for (int j = 0; j < attackPossibilities[i].length; j++){
				try{
					if (board[x+attackPossibilities[i][j][0]][y+attackPossibilities[i][j][1]] == null && board[x+attackPossibilities[i][j][0]/2][y+attackPossibilities[i][j][1]/2] != null) {
						if (board[x+attackPossibilities[i][j][0]/2][y+attackPossibilities[i][j][1]/2].isBlack != isBlack){
							multiJumpLocs.add(new Point(x+attackPossibilities[i][j][0], y+attackPossibilities[i][j][1]));
							validAttacks.add(new Point(x+attackPossibilities[i][j][0], y+attackPossibilities[i][j][1]));
						}
						break;
					}
				}catch( ArrayIndexOutOfBoundsException e ) {
					break;
				}
			}
		}
		
		return validAttacks;	
	}
	
	@Override
	public GamePiece[][] moveTo(int x_, int y_, GamePiece[][] board){
		board[x][y] = null;
		x = x_;
		y = y_;
		if ((!isBlack && y == 0) || (isBlack && y == 7)){
			board[x_][y_] = new KingCheckPiece(isBlack, x_, y_);
		}else{
			board[x_][y_] = this;
		}
		return board;
	}
	
	@Override
	public GamePiece[][] attackTo(int x_, int y_, GamePiece[][] board){
		board[(x+x_)/2][(y+y_)/2] = null;
		return moveTo(x_, y_, board);
	}
	
	public String getFilePath(){
		String filePath = "img/checkpieces/";
		
		if (isBlack){
			filePath += "black";
		}else {
			filePath += "red";
		}
		filePath += ".png";

		return filePath;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return isBlack?"black checker":"red checker";
	}
		
}